﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CarLoanSystem.Entity;
using System.Data;
using CarLoanSystem.DAL;
using CarLoanSystem.Web.Comman;

namespace CarLoanSystemWeb.Page.ChangePassword
{
    public partial class wuc_change_password : UserControlManageForm
    {
        private static CustomerItem cusItem;
        public event EventHandler InfoMessege;
        public event EventHandler ErrorMessege;
        public event EventHandler ErrorMessegeName;
        public event EventHandler ErrorMessegePassword;
        public static string message;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cusItem = Session["Member"] as CustomerItem;
                this.lblUserName.Text = cusItem.USERNAME;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                bool checkUser = CustomerInfoDAL.CheckUsernamePassword(this.lblUserName.Text, this.txtOldPassword.Text);
                if (checkUser)
                {
                    if (this.txtNewPassword.Text == this.txtNewPasswordConfirm.Text)
                    {
                        cusItem.PASSWORD = this.txtNewPassword.Text;
                        CustomerInfoDAL.ChangePassword(cusItem);
                        if (InfoMessege != null)
                        {
                            InfoMessege(this, EventArgs.Empty);
                        }
                    }
                    else 
                    {
                        if (ErrorMessegePassword != null)
                        {
                            ErrorMessegePassword(this, EventArgs.Empty);
                        }
                    }
                }
                else 
                {
                    if (ErrorMessegeName != null)
                    {
                        ErrorMessegeName(this, EventArgs.Empty);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ErrorMessege != null)
                {
                    ErrorMessege(this, EventArgs.Empty);
                }
            }
        }
    }
}